package com.by.service.impl;

import com.by.bean.GoodsCategory;
import com.by.common.core.crud.BaseServiceImpl;
import com.by.mapper.GoodsCategoryMapper;
import com.by.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsCategoryServiceImpl extends BaseServiceImpl<GoodsCategory, GoodsCategoryMapper> implements GoodsCategoryService {
    @Autowired
    GoodsCategoryMapper mapper;
    @Override
    public int saveCategory(GoodsCategory goodsCategory) {
        String maxCode = mapper.queryMaxCode(goodsCategory.getParentid());
        if (maxCode==null){
            //最大编码为空 默认赋值父编码拼接01
            goodsCategory.setCode(mapper.queryCodeById(goodsCategory.getParentid())+"01");
        }
        //最大编码不为空 最大编码加一作为新编码
        int code=Integer.parseInt(maxCode)+1;
        //新编码
        goodsCategory.setCode(code+"");
        System.out.println(goodsCategory);
        //添加新分类
        mapper.saveCategory(goodsCategory);
        return 1;
    }

    @Override
    public List<GoodsCategory> categorytree() {
        return mapper.categoryTree();
    }


}
